package com.whfc.ms.api.base.controller;

import com.whfc.base.entity.AppDocumentManage;
import com.whfc.common.base.BaseController;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.*;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.entity.dto.warn.OssFileInfoDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.utils.UserUtils;
import com.whfc.ms.api.base.service.DocumentManageService;
import com.whfc.ms.config.AbstractBaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (AppDocumentManage)表控制层
 *
 * @author makejava
 * @since 2022-07-13 15:30:47
 */
@RestController
@RequestMapping("/ms/api/appDocumentManage")
public class AppDocumentManageController extends AbstractBaseController {
    /**
     * 服务对象
     */
    @Resource
    private DocumentManageService documentManageService;



    /**
     * 通过项目Id查询所有文件夹
     *
     * @param deptId 项目Id
     * @return 所有文件夹
     */
    @GetMapping("folder")
    public R<List<AppDocumentManage>> queryAllFolderById(@RequestParam("deptId") Integer deptId) {
        return R.success(documentManageService.queryAllFolderById(deptId));
    }
    /**
     * 通过项目Id查询所有文件,分页
     *
     * @param deptId 项目Id
     * @return 所有文件的分页
     */
    @GetMapping("file")
    public R<PageVO<AppDocumentManage>> queryPageByDocType(@RequestParam("deptId") Integer deptId,
                                                        @RequestParam(value ="folderId") Integer folderId,
//                                                        @RequestParam(value = "name",required = false)  String name,
                                                        @RequestParam("pageNum") Integer pageNum,
                                                        @RequestParam("pageSize") Integer pageSize) {
        return R.success(documentManageService.queryPageByFolderId(deptId,folderId,pageNum,pageSize,null));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/info")
    public R<AppDocumentManage> queryById(@RequestParam("id") Integer id) {
        return R.success(this.documentManageService.queryById(id));
    }

    /**
     * 新增文件或文件夹
     *
     * @param appDocumentManage 实体
     * @return 新增结果
     */
    @PostMapping("add/document")
    public R<String> addFolder(@RequestBody AppDocumentManage appDocumentManage) {
        Integer userId = getLoginUser().getId();
        appDocumentManage.setCreateUser(userId);
        appDocumentManage.setUpdateUser(userId);
        return R.success(this.documentManageService.addFolder(appDocumentManage));
    }


    /**
     * 更新数据
     *
     * @param appDocumentManages 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R<String> edit(@RequestBody List<AppDocumentManage> appDocumentManages) {
        Integer userId = getLoginUser().getId();
        for (AppDocumentManage appDocumentManage: appDocumentManages) {
            appDocumentManage.setUpdateUser(userId);
        }
        return R.success(this.documentManageService.update(appDocumentManages));
    }

    /**
     * 删除数据
     *
     * @param ids 实体
     * @return 编辑结果
     */
    @PostMapping("/delete")
    public R<String> delete(@RequestBody List<Integer> ids) {
        List<AppDocumentManage> appDocumentManage = new ArrayList<>();
        ids.forEach(id->{
            appDocumentManage.add(AppDocumentManage.builder().id(id).delFlag(1).build());
        });
        return R.success(this.documentManageService.update(appDocumentManage));
    }


    /**
     * 文件上传
     */
    @PostMapping("/file/upload")
    public R<OssFileInfoDTO> uploadFile(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam(value = "file") MultipartFile file) throws IOException {
        logger.info("材料附件上传,loginer:{},file:{}", user.getUsername(), file.toString());
        OssFileInfoDTO ossPathDTO = documentManageService.uploadFile(file);
        return R.success(ossPathDTO);
    }


    @GetMapping("/file/anon/getConfig")
    public R getConfig(@RequestParam("name") String name) {
        logger.info("获取文件上传配置参数,name:{}", name);
        Map<String, String> map = documentManageService.getConfig(name);
        return R.success(map);
    }

    @GetMapping("/file/url")
    public R getConfig(@RequestParam("fileId") Integer fileId) {
        logger.info("获取文件下载链接,fileId:{}", fileId);
        Map<String, String> map = documentManageService.getFileUrl(fileId);
        return R.success(map);
    }

    /**
     * 通过项目Id查询所有文件,分页
     *
     * @param deptId 项目Id
     * @return 所有文件的分页
     */
    @GetMapping("file/search")
    public R<PageVO<AppDocumentManage>> queryPageByName(@RequestParam("deptId") Integer deptId,
                                                           @RequestParam(value ="folderId",required = false) Integer folderId,
                                                           @RequestParam(value = "name",required = false)  String name,
                                                           @RequestParam("pageNum") Integer pageNum,
                                                           @RequestParam("pageSize") Integer pageSize) {
        return R.success(documentManageService.queryFilePageByFolderId(deptId,folderId,pageNum,pageSize,name));
    }

    /**
     * 2.0文件重命名
     *
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/file/rename")
    public R rename(@RequestParam Integer id, @RequestParam String name) {
        documentManageService.rename(id, name);
        return R.success();
    }
}

